package com.card.log;

import com.card.user.UserBase;
import com.xiaoao.net.db.Entity;
import com.xiaoao.net.db.mongo.MongodbManager;
import com.xiaoao.net.utils.PrimaryKey;
import com.xiaoao.net.utils.PubFunc;
import com.xiaoao.net.utils.StringUtil;
import com.xiaoao.net.utils.TimeUtils;

public class UserGoldLog {
	private String id;
	private String date;
	private String userId;
	private int logType;
	private int spend;
	private String info;
	
	public UserGoldLog(UserBase user,int spend, LogType logType) {
		this.id = PrimaryKey.getPrimaryKey("UserGoldLog");
		this.date = TimeUtils.GetCurrentTimeStamp();
		this.userId = user.getPrimaryKey();
		this.logType = logType.getType();
		this.spend = spend;
	}
	public UserGoldLog(UserBase user,int spend, String info) {
		this.id = PrimaryKey.getPrimaryKey("UserGoldLog");
		this.date = TimeUtils.GetCurrentTimeStamp();
		this.userId = user.getPrimaryKey();
		this.info = info;
		this.spend = spend;
	}
	public UserGoldLog(Entity entity) {
		this.id = entity.getKey().getName();
		this.date = (String)entity.getProperty("date");
		this.userId = (String)entity.getProperty("userId");
		this.logType = PubFunc.parseInt(entity.getProperty("logType"));
		this.spend = (int)entity.getProperty("spend");
		this.info = entity.hasProperty("info") ? (String)entity.getProperty("info"):null;
	}
	
	public void save() {
		Entity entity = new Entity("UserGoldLog", this.id);
		entity.setProperty("id", id);
		entity.setProperty("date", date);
		entity.setIndexedProperty("userId", userId);
		entity.setProperty("logType", logType);
		entity.setProperty("spend", spend);
		entity.setProperty("info", info);
		entity.setProperty("addTime", System.currentTimeMillis());
		MongodbManager.getInstance().saveOrUpdate(entity);
	}
	
	public String getDate() {
		return date;
	}
	public int getLogType() {
		return logType;
	}
	public int getSpend() {
		return spend;
	}
	
	public String getEvent() {
		if(!StringUtil.NullOrEmpty(info)) {
			return info;
		}
		LogType lt = LogType.getLogType(logType);
		if(lt != null) {
			return lt.getDesc();
		}
		return "未知";
	}
}
